package com.zpc.transformfile;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author : zpc20 //作者
 * @version : 1.0 // 版本
 * @ClassName : Server  //类名
 * @Description : 服务端发送文件  //描述
 * @Date: 2024-11-24  下午 10:58
 */
public class Server {
    public static void main(String[] args) {
        try(ServerSocket serverSocket = new ServerSocket(8081)){
            System.out.println("等待接收文件");
            Socket socket = serverSocket.accept();
            System.out.println("接收文件中...");
            InputStream in = socket.getInputStream();
            FileOutputStream fileOutputStream = new FileOutputStream("net/upload/temp.txt");
            byte[] buffer = new byte[1024];
            int len = 0;
            while((len=in.read(buffer))!=-1){
                fileOutputStream.write(buffer,0,len); // 输出到文件
            }
            System.out.println("文件接收完毕!!");
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
